package com.mankind.ssm.controller;

import com.mankind.ssm.common.CONSTANT;
import com.mankind.ssm.common.Page;
import com.mankind.ssm.pojo.Permission;
import com.mankind.ssm.pojo.Role;
import com.mankind.ssm.pojo.RolePermission;
import com.mankind.ssm.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Mankind on 2017/7/20.
 */
@Controller
@RequestMapping("/permission")
public class PermissionController {
    @Autowired
    private UserService userService;
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private UserRoleService userRoleService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private RolePermissionService rolePermissionService;
    @Autowired
    private PermissionService permissionService;
    @ResponseBody
    @RequestMapping(value="/getPermissionTotalPages",method= RequestMethod.POST,produces="application/json;charset=UTF-8")
    public Map getPermissionTotalPages(HttpServletRequest request){
        Map<String,Object> json=new HashMap<String, Object>();
        //如果是带条件查询
        String text=request.getParameter("text");
        Integer totalPages=null;
        if(text==null||text==""){
            totalPages=permissionService.getTotalNumber();
        }else{
            totalPages=permissionService.getTotalNumber(text);
        }
        json.put("totalPages",totalPages);
        return json;
    }
    @ResponseBody
    @RequestMapping(value="/getPermissionByPage",method= RequestMethod.POST,produces="application/json;charset=UTF-8")
    public Map getPermissionByPage(HttpServletRequest request){
        Map<String,Object> json=new HashMap<String, Object>();
        Integer currentPage=Integer.parseInt(request.getParameter("Page"));
        String text=request.getParameter("text");
        Page<Permission> page=null;
        System.out.println("page前："+text);
        if(text==null||text==""){
            page=permissionService.getRoleByPage(CONSTANT.USERINFO_LIST_SIZE,currentPage);
        }else{
            page=permissionService.fuzzyQueryByPage(currentPage,CONSTANT.USERINFO_LIST_SIZE,text);
        }
        System.out.println("text:"+text+ " ----"+page.getObj().get(0).getResourceUrl());
        json.put("permissionList",page.getObj());
        return json;
    }
    @ResponseBody
    @RequestMapping(value="/loadPermission",method= RequestMethod.POST,produces="application/json;charset=UTF-8")
    public Map loadPermission(HttpServletRequest request){
        Map<String,Object> json=new HashMap<String, Object>();
//        Integer id=(Integer)request.getSession().getAttribute("userId");
        Integer permissionId=null;
        String x=request.getParameter("id");
        Permission permission=null;
        if(x!=null||x!=""){
            permissionId=Integer.parseInt(x);
            permission=permissionService.getPermissionById(permissionId);
        }
        json.put("permission",permission);
        return json;
    }
    @ResponseBody
    @RequestMapping(value="/createPermission",method= RequestMethod.POST,produces="application/json;charset=UTF-8")
    public void createPermission(HttpServletRequest request){
        String resourceName=request.getParameter("resourceName");
        String resourceUrl=request.getParameter("resourceUrl");
        Permission permission=new Permission(resourceName,resourceUrl);
        permissionService.insertPermission(permission);
    }
    @ResponseBody
    @RequestMapping(value="/deletePermission",method= RequestMethod.POST,produces="application/json;charset=UTF-8")
    public void deletePermission(HttpServletRequest request){
        Integer id=Integer.parseInt(request.getParameter("id"));
        System.out.println("准备删除权限id:"+id);
        permissionService.deleteByPermissionId(id);
        rolePermissionService.deleteByPermissionId(id);
    }
    @ResponseBody
    @RequestMapping(value="/updatePermission",method= RequestMethod.POST,produces="application/json;charset=UTF-8")
    public void updatePermission(HttpServletRequest request){
        Integer id=Integer.parseInt(request.getParameter("id"));
        Permission permission=permissionService.getPermissionById(id);
        String resourceName=request.getParameter("resourceName");
        String resourceUrl=request.getParameter("resourceUrl");
        permission.setResourceName(resourceName);
        permission.setResourceUrl(resourceUrl);
        permissionService.updatePermission(permission);
    }
}
